package day10;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;

public class parseId {

    public int parseId(String id){
        // 1. 格式校验
        String idRegex="^\\d{17}[0-9Xx]$";
        if(!id.matches(idRegex)){
            throw new RuntimeException("身份证格式不正确");
        }
        // 2. 获得日期
        //3.解析出生时间
        String birth = id.substring(6, 14);
        //日期的计算
        //字符串转LocalDate
        LocalDate birthDate = LocalDate.parse(birth, DateTimeFormatter.ofPattern("yyyyMMdd"));
        //获得现在时间
        LocalDate now = LocalDate.now();
        //获得今年的生日时间
        LocalDate birthday = birthDate.withYear(now.getYear());

        Period period = Period.between(birthDate, now);
        int age = period.getYears();
        return age;
    }
}
